package Game;

import static org.lwjgl.opengl.GL11.*;

import java.util.Random;

import org.newdawn.slick.opengl.Texture;

import Engine.Matrix3D;
import Engine.Vector3D;


public class Quadrados extends GameObject {

	public float rotAngleX, rotAngleY, rotAngleZ,size;
	Texture texture = null;

	private boolean[] isTranslating = new boolean[3];
	private boolean[] isRotating = new boolean[3];

	public Quadrados(float x, float y, float z,float size,Texture texture) {
		super(x, y, z);
		// TODO Auto-generated constructor stub
		this.size = size;
		this.texture = texture;

		Random rand = new Random();
	}

	public boolean isSpacePress = false;
	Matrix3D matrix = new Matrix3D();
	Vector3D vector = new Vector3D(x, y, z);
	@Override
	public void Render() {
		// TODO Auto-generated method stub
//		glColor3f(1.0f, 1.0f, 1.0f);

		if(texture!=null){
			texture.bind();
		}

		if(!isSpacePress){
			glPushMatrix();
			{
				glTranslatef(x, y, z);
				glRotatef (rotAngleX, 1.0f, 0.0f, 0.0f);
				glRotatef (rotAngleY, 0.0f, 1.0f, 0.0f);
				glRotatef (rotAngleZ, 0.0f, 0.0f, 1.0f);

				glTranslatef(-0.5f,-.5f , 0.0f);

				glBegin(GL_TRIANGLE_STRIP);
				{
					glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(0.0f, 1.0f);
					glVertex3f(0, 0, 0);

					glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(1.0f, 1.0f);
					glVertex3f(1, 0, 0);

					glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(0.0f, 0.0f);
					glVertex3f(0, 1, 0);

					glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(1.0f, 0.0f);
					glVertex3f(1, 1, 0);

					//normais negativas
					float contracapa = -0.001f;
					glNormal3f(0.0f, 0.0f, -1.0f);
					glTexCoord2f(0.0f, 1.0f);
					glVertex3f(0, 0, contracapa);

					glNormal3f(0.0f, 0.0f, -1.0f);
					glTexCoord2f(1.0f, 1.0f);
					glVertex3f(1, 0, contracapa);

					glNormal3f(0.0f, 0.0f, -1.0f);
					glTexCoord2f(0.0f, 0.0f);
					glVertex3f(0, 1, contracapa);

					glNormal3f(0.0f, 0.0f, -1.0f);
					glTexCoord2f(1.0f, 0.0f);
					glVertex3f(1, 1, contracapa);
				}
				glEnd();
			}
			glPopMatrix();
			
		} else {

			glPushMatrix();
			{
				matrix.translate(vector);
				matrix.setRotate(rotAngleX, 1.0f, 0.0f, 0.0f);
				matrix.setRotate(rotAngleY, 0.0f, 1.0f, 0.0f);
				matrix.setRotate(rotAngleZ, 0.0f, 0.0f, 1.0f);
				matrix.translate(new Vector3D(-0.5f,-.5f , 0.0f));

				glBegin(GL_QUAD_STRIP);
				{
					//glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(0.0f, 1.0f);
//					glVertex3f(matrix.m00, matrix.m01, matrix.m02);
					glVertex3f(0, 0, 0);

					//glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(1.0f, 1.0f);
//					glVertex3f(matrix.m10, matrix.m11, matrix.m13);
					glVertex3f(1, 0, 0);

					//glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(0.0f, 0.0f);
					glVertex3f(0, 1, 0);
//					glVertex3f(matrix.m20, matrix.m21, matrix.m23);

//					glNormal3f(0.0f, 0.0f, 1.0f);
					glTexCoord2f(1.0f, 0.0f);
					glVertex3f(1, 1, 0);
//					glVertex3f(matrix.m30, matrix.m31, matrix.m33);

					//normais negativas
//					float contracapa = -0.001f;
//					glNormal3f(0.0f, 0.0f, -1.0f);
//					glTexCoord2f(0.0f, 1.0f);
//					glVertex3f(0, 0, contracapa);
//
//					glNormal3f(0.0f, 0.0f, -1.0f);
//					glTexCoord2f(1.0f, 1.0f);
//					glVertex3f(1, 0, contracapa);
//
//					glNormal3f(0.0f, 0.0f, -1.0f);
//					glTexCoord2f(0.0f, 0.0f);
//					glVertex3f(0, 1, contracapa);
//
//					glNormal3f(0.0f, 0.0f, -1.0f);
//					glTexCoord2f(1.0f, 0.0f);
//					glVertex3f(1, 1, contracapa);
				}
				glEnd();
			}
			glPopMatrix();
		}
	}

	boolean invertX = new Random().nextBoolean();
	boolean invertY = new Random().nextBoolean();
	float vel = new Random().nextFloat() / 1000 + .002f;
	@Override
	public void Update(long diftime) {

		if(isRotating[0])
			rotAngleX+=(diftime/10.0f);
		if(isRotating[1])
			rotAngleY+=(diftime/10.0f);
		if(isRotating[2])
			rotAngleZ+=(diftime/10.0f);

		if(invertX)
			x+= vel;
		else
			x-= vel;

		if(invertY)
			y+= vel;
		else
			y-= vel;

		if(x<-10)
			invertX = true;
		if(x>10)
			invertX = false;

		if(y<-5)
			invertY = true;
		if(y>5)
			invertY = false;
	}
}